<?php

namespace App\Http\Controllers\common;
/*
* @Author: 李明
* @Date: 2021-08-10 14:11:12
 * @LastEditTime: 2022-01-13 09:00:49
 * @LastEditors: 李明
* @Description:
 * @FilePath: /CRM2.0-PHP/app/Http/Controllers/common/CommonFun.php
* @山东壹零贰肆信息科技有限公司
*/

use Illuminate\Support\Facades\Redis;

class CommonFun
{

    //md5额外加密
    static function md5($data)
    {
        return md5("!@#$%^&*(OKJNBVFDIOIUYTRESXCVBNM<LL<MNBFDW@#$%^&IKMNBVCXSA!@#$%TYJNBVCFUIUYTERG" . $data);
    }
    /**
     * @description:
     * @param {*} $data
     * @return {*}
     */
    public static function GetTokenData($data)
    {
        $token = explode(".", $data);
        $token = $token[1];
        $token = base64_decode($token);
        $userinfo = json_decode($token, 1);
        return $userinfo;
    }
    /**
     * @description:
     * @param {*} $id
     * @param {*} $channel  1员工PC端  2员工小程序端  3客户APP iOS端  4安卓端
     * @return {*}
     */
    public static function TokenKeyCreate($id, $channel = "1")
    {
        switch ($channel) {
            case '1': // 员工PC端
                $keyToken_worker = 'AUTH_TOKEN_' . $id;
                break;
            case '2': // 员工小程序端
                $keyToken_worker = 'XCX_TOKEN_' . $id;
                break;
            case '3': // 客户APPiOS端
                $keyToken_worker = 'USER_TOKEN_' . $id;
                break;
            case '4': // 客户APPiOS端
                $keyToken_worker = 'USER_TOKEN_' . $id;
                break;
            default:
                $keyToken_worker = 'AUTH_TOKEN_' . $id;
                break;
        }
        // dd($keyToken_worker);
        return $keyToken_worker;
    }
    /**
     * @description: 使用递归 转成treelist
     * @param {*} $list 数据列表
     * @param {*} $id 子id的key
     * @param {*} $pid 父 id的key
     * @param {*} $son 子列表的key
     * @return {*}
     */
    public static function ArrToTreeList($list, $id = 'id', $pid = 'pid', $son = 'treeList')
    {
        list($tree, $map) = [[], []];
        foreach ($list as $item) {
            $map[$item[$id]] = $item;
        }
        foreach ($list as $item) {
            if (isset($item[$pid]) && isset($map[$item[$pid]])) {
                $map[$item[$pid]][$son][] = &$map[$item[$id]];
            } else {
                $tree[] = &$map[$item[$id]];
            }
        }
        unset($map);
        return $tree;
    }
    /**
     * 一维数据数组生成数据树
     * @param array $list 数据列表
     * @param string $id 父ID Key
     * @param string $pid ID Key
     * @param string $son 定义子数据Key
     * @return Collection
     */
    public static function arr2tree($list, $id = 'id', $pid = 'pid', $son = 'treeList')
    {
        list($tree, $map) = [[], []];
        foreach ($list as $item) {
            $map[$item[$id]] = $item;
        }

        foreach ($list as $item) {
            if (isset($item[$pid]) && isset($map[$item[$pid]])) {
                $map[$item[$pid]][$son][] = &$map[$item[$id]];
            } else {
                $tree[] = &$map[$item[$id]];
            }
        }
        unset($map);
        return $tree;
    }

    public static function ArrToTreeNew($list, $top, $id = 'id', $pid = 'pid', $tree = 'treeList')
    {
        $treeList = [];
        foreach ($list as $key => $item) {
            if ($item[$pid] == $top[$id]) {
                $treeList[] = $item;
                unset($item);
            }
        }
        $top[$tree] = $treeList;
        foreach ($top[$tree] as $key => $item) {
            CommonFun::ArrToTreeNew($list, $top[$tree]);
        }
        return $top;
    }


    /**
     * @description: 返回 list 里面 pid 的 子孙们，id集合，一般用于wherein条件
     * @param {*} $arrList
     * @param {*} $pid
     * @return string 逗号分割的字符串
     */
    public static function PidChildList($arrList, $pid)
    {
        $arr = '';
        foreach ($arrList as $key => $value) {
            if ($value['id'] == $pid || $value['pid'] == $pid) {
                $arr .= $value['id'] . ",";
                unset($arrList[$key]); //去除多余的数据
                $id = self::PidChildList($arrList, $value['id']);
                if (!empty($id)) {
                    $arr .= $id . ",";
                }
            }
        }
        return trim($arr, ',');
    }

    /**
     * @description:  根据传入的 model_list 和 pid  返回 pid的子集 id数组 一般用于wherein条件
     * @param array $arrListModel
     * @param string $pid
     * @return array id数组
     */
    public static function PidChildModelList($arrListModel, $pid)
    {
        /*写的对不对还未验证，需要测试*/
        $arrList = [];
        foreach ($arrListModel as $key => $model) {
            $id = $model->id;
            if ($id == $pid || $model->pid == $pid) {
                $arrList[] = $id;
                // 删除 已经处理了的数据
                $arrListModel = array_diff($arrListModel, $model);
                $arrList = array_merge($arrList, self::PidChildModelList($arrListModel, $id));
            }
        }

        return $arrList;
    }
    // 把数据库查询出来的数据转成一维数组，仅针对一个字段
    public static function SelSqlArr($kwStr, $arrSql)
    {

        $arr = [];
        // dd($arrSql);
        foreach ($arrSql as $k => $v) {
            $arr[] = $v[$kwStr];
        }
        // dd($arr);
        return $arr;
    }
    /**
     * @description: 根据出生年月日计算年龄
     * @param string $mydate
     * @return {*}
     */
    public static function birthday($mydate)
    {
        try {
            $birth = $mydate;
            if ($birth != '') {
                list($by, $bm, $bd) = explode('-', $birth);
                $cm = date('n');
                $cd = date('j');
                $age = date('Y') - $by - 1;
                if ($cm > $bm || $cm == $bm && $cd > $bd) $age++;
                return $age;
            } else {
                return '';
            }
        } catch (\Throwable $th) {
            return '';
        }
    }
    /**
     * @description: 传入model_list  返回  id_list
     * @param array $model_list
     * @param string $id 要返回的  参数list ，默认是  id
     * @return array
     */
    public static function id_list_model($model_list, $id = 'id')
    {
        $id_list = [];
        foreach ($model_list as $model) {
            $id_list[] = $model->$id;
        }
        return $id_list;
    }

    public static function str_change($data)
    {
        //去除一个字符串反斜杠
        $data = stripslashes($data);
        // 去除一个字符串两端空格
        $data = trim($data);
        // 解码
        $data = json_decode($data, true);
        return $data;
    }
}
